# 目标模块名称
obj-m += pcie_mcdev.o
# 指定模块由多个源文件组成
pcie_mcdev-objs := drv_pcie.o drv_pcie_irq.o drv_cdev.o drv_xcan.o drv_xcan_dev.o \
                   drv_ai.o drv_pcie_dma.o drv_di.o drv_pi.o drv_do.o drv_ao.o drv_pt100.o \
                   drv_mcdev.o drv_ecum.o drv_acm.o drv_dim.o drv_dom.o drv_aim.o drv_aom.o \
                   drv_pim.o drv_ptm.o drv_iepem.o drv_soem.o \

# 硬件版本定义
HARD_VER ?= 0
# 软件版本信息
DRV_VER_MAJOR := 1
DRV_VER_MINOR := 0
DRV_VER_PATCH := 0
CANFD_VER_MAJOR := $(DRV_VER_MAJOR)
CANFD_VER_MINOR := $(DRV_VER_MINOR)
CANFD_VER_PATCH := $(DRV_VER_PATCH)
CAN_VER_MAJOR := $(DRV_VER_MAJOR)
CAN_VER_MINOR := $(DRV_VER_MINOR)
CAN_VER_PATCH := $(DRV_VER_PATCH)
DI_VER_MAJOR := $(DRV_VER_MAJOR)
DI_VER_MINOR := $(DRV_VER_MINOR)
DI_VER_PATCH := $(DRV_VER_PATCH)
DO_VER_MAJOR := $(DRV_VER_MAJOR)
DO_VER_MINOR := $(DRV_VER_MINOR)
DO_VER_PATCH := $(DRV_VER_PATCH)
AI_VER_MAJOR := $(DRV_VER_MAJOR)
AI_VER_MINOR := $(DRV_VER_MINOR)
AI_VER_PATCH := $(DRV_VER_PATCH)
AO_VER_MAJOR := $(DRV_VER_MAJOR)
AO_VER_MINOR := $(DRV_VER_MINOR)
AO_VER_PATCH := $(DRV_VER_PATCH)
PI_VER_MAJOR := $(DRV_VER_MAJOR)
PI_VER_MINOR := $(DRV_VER_MINOR)
PI_VER_PATCH := $(DRV_VER_PATCH)
PT_VER_MAJOR := $(DRV_VER_MAJOR)
PT_VER_MINOR := $(DRV_VER_MINOR)
PT_VER_PATCH := $(DRV_VER_PATCH)

# 编译模式：可选值 cross(交叉编译) 或 native(本地编译)
COMPILE_MODE ?= cross

# 芯片平台：可选值 rk3588, rk3568
SOC_PLATFORM ?= rk3568

# 目标架构
ARCH ?= arm64

# 根据编译模式和芯片平台设置工具链和内核路径
ifeq ($(COMPILE_MODE), cross)
    $(info Building for $(SOC_PLATFORM) using cross-compilation)
    ifeq ($(SOC_PLATFORM), rk3588)
        CROSS_COMPILE   ?= /media/test/sde/ubuntu2204/rk3588/tl3588-evm_buildroot-2021.11_v2.2/rk3588_linux_release/prebuilts/gcc/linux-x86/aarch64/gcc-arm-10.3-2021.07-x86_64-aarch64-none-linux-gnu/bin/aarch64-none-linux-gnu-
        KERNEL_DIR      ?= /media/test/sde/ubuntu2204/rk3588/tl3588-evm_buildroot-2021.11_v2.2/rk3588_linux_release/kernel
        EXTRA_CFLAGS    += -DTARGET_RK3588
    else ifeq ($(SOC_PLATFORM), rk3568)
        CROSS_COMPILE   ?= /media/test/sde/ubuntu2204/rk3568/tl3568f-evm_v1.5/rk356x_linux_release_v1.3.1/prebuilts/gcc/linux-x86/aarch64/gcc-arm-10.3-2021.07-x86_64-aarch64-none-linux-gnu/bin/aarch64-none-linux-gnu-
        KERNEL_DIR      ?= /media/test/sde/ubuntu2204/rk3568/tl3568f-evm_v1.5/rk356x_linux_release_v1.3.1/kernel
        EXTRA_CFLAGS    += -DTARGET_RK3568
    else
        $(error Unsupported SOC_PLATFORM for cross compilation: $(SOC_PLATFORM). Supported values: rk3588, rk3568)
    endif
else ifeq ($(COMPILE_MODE), native)
    $(info Building natively for $(SOC_PLATFORM))
    CROSS_COMPILE       ?=
    ifeq ($(SOC_PLATFORM), rk3588)
        EXTRA_CFLAGS    += -DTARGET_RK3588
    else ifeq ($(SOC_PLATFORM), rk3568)
        EXTRA_CFLAGS    += -DTARGET_RK3568
    else
        $(error Unsupported SOC_PLATFORM for native compilation: $(SOC_PLATFORM). Supported values: rk3588, rk3568)
    endif
else
    $(error Unsupported COMPILE_MODE: $(COMPILE_MODE). Supported values: cross, native)
endif

# 编译选项
EXTRA_CFLAGS += -Wall -DHARD_VER=$(HARD_VER) \
                -DCANFD_VER_MAJOR=$(CANFD_VER_MAJOR) \
                -DCANFD_VER_MINOR=$(CANFD_VER_MINOR) \
                -DCANFD_VER_PATCH=$(CANFD_VER_PATCH) \
                -DCAN_VER_MAJOR=$(CAN_VER_MAJOR) \
                -DCAN_VER_MINOR=$(CAN_VER_MINOR) \
                -DCAN_VER_PATCH=$(CAN_VER_PATCH) \
                -DDI_VER_MAJOR=$(DI_VER_MAJOR) \
                -DDI_VER_MINOR=$(DI_VER_MINOR) \
                -DDI_VER_PATCH=$(DI_VER_PATCH) \
                -DDO_VER_MAJOR=$(DO_VER_MAJOR) \
                -DDO_VER_MINOR=$(DO_VER_MINOR) \
                -DDO_VER_PATCH=$(DO_VER_PATCH) \
                -DAI_VER_MAJOR=$(AI_VER_MAJOR) \
                -DAI_VER_MINOR=$(AI_VER_MINOR) \
                -DAI_VER_PATCH=$(AI_VER_PATCH) \
                -DAO_VER_MAJOR=$(AO_VER_MAJOR) \
                -DAO_VER_MINOR=$(AO_VER_MINOR) \
                -DAO_VER_PATCH=$(AO_VER_PATCH) \
                -DPI_VER_MAJOR=$(PI_VER_MAJOR) \
                -DPI_VER_MINOR=$(PI_VER_MINOR) \
                -DPI_VER_PATCH=$(PI_VER_PATCH) \
                -DPT_VER_MAJOR=$(PT_VER_MAJOR) \
                -DPT_VER_MINOR=$(PT_VER_MINOR) \
                -DPT_VER_PATCH=$(PT_VER_PATCH) \

all:
	$(MAKE) ARCH=$(ARCH) CROSS_COMPILE=$(CROSS_COMPILE) -C $(KERNEL_DIR) M=$(PWD) EXTRA_CFLAGS="$(EXTRA_CFLAGS)" modules

clean:
	$(MAKE) ARCH=$(ARCH) CROSS_COMPILE=$(CROSS_COMPILE) -C $(KERNEL_DIR) M=$(PWD) clean

# 发布版本
release: EXTRA_CFLAGS += -DNDEBUG
release: all
	@echo "=== 发布版本构建完成 ==="

# 调试版本
debug: EXTRA_CFLAGS += -g -DDEBUG
debug: all
	@echo "=== 调试版本构建完成 ==="

# 显示构建信息
info:
	@echo "=== 构建信息 ==="
	@echo "模块: pcie_do"
	@echo "源文件: $(pcie_do-objs)"
	@echo "编译模式: $(COMPILE_MODE)"
	@echo "芯片平台: $(SOC_PLATFORM)"
	@echo "目标架构: $(ARCH)"
	@echo "内核路径: $(KERNEL_DIR)"
	@echo "交叉编译工具链: $(if $(CROSS_COMPILE),$(CROSS_COMPILE),使用系统gcc)"
	@echo "================="

# 显示帮助信息
help:
	@echo "可用目标:"
	@echo "  all     - 构建内核模块"
	@echo "  clean   - 清理构建文件"
	@echo "  info    - 显示构建信息"
	@echo "  help    - 显示此帮助信息"
	@echo ""
	@echo "构建选项:"
	@echo "  COMPILE_MODE=cross|native    # 指定编译模式（默认: cross）"
	@echo "  SOC_PLATFORM=rk3588|rk3568   # 指定芯片平台（默认: rk3588）"

.PHONY: all clean info help
